package com.facebook.a.a;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public final class a {
    private static final FileFilter a;

    static {
        a = new b();
    }

    public static int a() {
        int v0;
        int v1 = -1;
        if (Build.VERSION.SDK_INT <= 10) {
            v0 = 1;
            return v0;
        }

        try {
            v0 = a("/sys/devices/system/cpu/possible");
            if (v0 == v1) {
                v0 = a("/sys/devices/system/cpu/present");
            }
            if (v0 != v1) {
                return v0;
            }

            v0 = new File("/sys/devices/system/cpu/").listFiles(a).length;
        } catch (NullPointerException v0_1) {
            v0 = v1;
        } catch (SecurityException v0_2) {
            v0 = v1;
        }

        return v0;
    }

    private static int a(String arg4) {
        int v0 = -1;
        try {
            BufferedReader v2 = new BufferedReader(new InputStreamReader(new FileInputStream(arg4)));
            String v1_1 = v2.readLine();
            v2.close();
            if (v1_1 == null) {
                return v0;
            }
            if (!v1_1.matches("0-[\\d]+$")) {
                return v0;
            }
            v0 = Integer.valueOf(v1_1.substring(2)).intValue() + 1;
        } catch (IOException v1) {
        }

        return v0;
    }

    private static int a(String arg10, FileInputStream arg11) {
        int v2;
        int v0 = 0;
        int v1 = -1;
        int v9 = 1024;
        int v8 = 10;
        byte[] v3 = new byte[v9];
        try {
            int v4 = arg11.read(v3);
            while (true) {
                if (v0 >= v4) {
                    return v1;
                }

                if (v3[v0] == v8 || v0 == 0) {
                    if (v3[v0] == v8) {
                        ++v0;
                    }

                    v2 = v0;
                    while (true) {
                        if (v2 >= v4) {
                            break;
                        }

                        int v5 = v2 - v0;
                        if (v3[v2] != arg10.charAt(v5)) {
                            ++v0;
                            break;
                        }

                        if (v5 != arg10.length() - 1) {
                            ++v2;
                            continue;
                        }

                        while (true) {
                            if (v2 < v9 && v3[v2] != v8) {
                                if (Character.isDigit(v3[v2])) {
                                    v0 = v2 + 1;
                                    while (v0 < v9) {
                                        if (!Character.isDigit(v3[v0])) {
                                            break;
                                        }
                                        ++v0;
                                    }
                                    return Integer.parseInt(new String(v3, 0, v2, v0 - v2));
                                } else {
                                    ++v2;
                                    continue;
                                }
                            }
                            return v1;
                        }
                    }
                }
                ++v0;
            }
        } catch (NumberFormatException v0_1) {
            return v1;
        } catch (IOException v0_2) {
            return v1;
        }
    }

    @TargetApi(value = 16)
    public static long a(Context context) {
        int v2_1;
        FileInputStream v4 = null;
        long v0;
        if (Build.VERSION.SDK_INT >= 16) {
            ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(memoryInfo);
            v0 = memoryInfo.totalMem;
            return v0;
        }

        long v2 = -1;
        try {
            v4 = new FileInputStream("/proc/meminfo");
            v2_1 = 10;
            v2 = (((long) a("MemTotal", v4))) << v2_1;
            return v2;
        } catch (Throwable v0_2) {
            try {
                v4.close();
            } catch (IOException v0_1) {

            }
            return v2;
        }
    }

    public static int b() {
        FileInputStream v3;
        FileInputStream v7;
        byte[] v6;
        int v9 = 128;
        int v1 = -1;
        int v4 = 0;
        int v0 = v1;
        try {
            while (true) {
                if (v4 >= a()) {
                    if (v0 != v1) {
                        return v0;
                    }
                    v3 = new FileInputStream("/proc/cpuinfo");
                    int v2_1 = a("cpu MHz", v3) * 1000;
                    if (v2_1 <= v0) {
                        return v0;
                    }
                    return v2_1;
                }
                File v5 = new File("/sys/devices/system/cpu/cpu" + v4 + "/cpufreq/cpuinfo_max_freq");
                if (!v5.exists()) {
                    ++v4;
                    continue;
                }
                v6 = new byte[128];
                v7 = new FileInputStream(v5);
                try {
                    v7.read(v6);
                    int v2_1;
                    for (v2_1 = 0; Character.isDigit(v6[v2_1]); ++v2_1) {
                        if (v2_1 >= v9) {
                            break;
                        }
                    }
                    Integer v2_2 = Integer.valueOf(Integer.parseInt(new String(v6, 0, v2_1)));
                    if (v2_2.intValue() > v0) {
                        v0 = v2_2.intValue();
                    }
                } catch (NumberFormatException e) {

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        v7.close();
                    }catch (Exception e){

                    }
                }
                ++v4;
            }
        } catch (IOException v0_1) {
            return v1;
        }
    }
}

